package com.txsj.scan.presenter;

import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.AllocationListBean;
import com.txsj.scan.bean.BaseListResult;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.view.AllocateHistoryIView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class AllocateHistoryPresenter extends BasePresenter<AllocateHistoryIView> {
    public AllocateHistoryPresenter(AllocateHistoryIView view) {
        super(view);
    }

    @Override
    protected void initModel() {

    }

    //获取调拨单列表
    public void getAllocationList(int out_warehouse_id, int page) {

        Map<String, String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID, ""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID, 0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN, ""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("out_warehouse_id", out_warehouse_id + "");//调出仓库id 默认是零
        params.put("limit", 20 + "");// 每页显示数量
        params.put("page", page + "");//页面的索引
        RetrofitManager.getDefault().create(ApiServer.class).getAllocateHistory(RetrofitManager.getHeader(), params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseListResult<AllocationListBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseListResult<AllocationListBean> result) {
                        if (result.isSuccess()) {
                            if (null != result.getData()) {
                                view.getAllocationListSuccess(result.getData());
                            } else {
                                view.getAllocationListFail("请求失败  请稍后重试");
                            }
                        } else {
//                            view.onError("提交失败，请重试");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
//                        view.onError("网络异常，请重试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
